package diamondRingServlet;

import diamondRingEntity.Admin;
import diamondRingService.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/*
修改密码
 */
@WebServlet("/BackUpdateServlet")
public class BackUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        PrintWriter out=response.getWriter();

        AdminServiceImpl adminService = new AdminServiceImpl();

        int count;

        String oldPwd = null;
        oldPwd =(String)session.getAttribute("mima");
        String name = null;
        name =(String)session.getAttribute("user");

        String oldPassword = request.getParameter("oldPassword");

        String reNewPassword = request.getParameter("reNewPassword");

        Admin admin = new Admin(name,reNewPassword);

        if (oldPwd.equals(oldPassword)){
            count = adminService.UpdatePassword(admin);
        }else{
            count = -1;
        }
        out.print(count);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
